﻿<%@ WebHandler Language="C#" Class="publicHouseAnalyse" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using Chenxin.Lib.Utility;
using System.Data;
using System.Data.OleDb;

public class publicHouseAnalyse : AjaxHandlerBase
{

    //*******************************************
    //功能概要       ：重写的请求处理方法
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：父类中隐藏了IHttpHandler接口方法，本类中须对本方法进行重写
    //作成者         ：chenyg
    //作成日         ：2010-05-04 
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        string strOp = context.Request["op"];
        string strResponse = "";
        switch (strOp.ToLower())
        {
            case "getlist":
                strResponse = GetList(context);
                break;
            default:
                break;
        }
        context.Response.Write(strResponse);
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    private string GetList(HttpContext context)
    {
       // string community = context.Request["community"];
       // string region = context.Request["region"];
       // string block = context.Request["block"];
       // string houseuse = context.Request["houseuse"];
        string type = context.Request["typeanlyse"];
        int iPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1:int.Parse(context.Request["pageindex"]) +1;//页码
        int iPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30:int.Parse(context.Request["pagesize"]);//页记录数

        System.Text.StringBuilder sb = new StringBuilder();
        sb.Append(" (1=1) ");
        /*
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_PublicHouse_ViewAll))
        {
            sb.Append(" (1=1) ");
        }
         //查看城市房源权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PublicHouse_ViewCity))
        {
            sb.Append("EXISTS(");
            sb.Append("Select 1 from v_Hr_User_BaseInfo M ");
            sb.AppendFormat("WHERE House_PublicDatum.AddPersonId = M.Userid AND  M.CityId= '{0}' ", CurrentUserInfo.CityId);
            sb.Append(")");
        }
        //查看公司房源权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PublicHouse_ViewCompany))
        {
            sb.Append("EXISTS(");
            sb.Append("Select 1 from v_Hr_User_BaseInfo M ");
            sb.AppendFormat("WHERE House_PublicDatum.AddPersonId = M.Userid AND  M.CompanyId= '{0}' ", CurrentUserInfo.CompanyId);
            sb.Append(")");
        }
        //查看部门房源权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PublicHouse_ViewDepart))
        {
            sb.Append("EXISTS(");
            sb.Append("Select 1 from v_Struct_UserDepartRelation M ");
            sb.AppendFormat("WHERE (House_PublicDatum.AddDepartId = M.Departid AND  M.Userid= '{0}') ", CurrentUserInfo.UserId);
            sb.Append(")");
        }
        else
        {
            sb.AppendFormat(" AddPersonId = '{0}'", CurrentUserInfo.UserId);
        }
        
        if (!string.IsNullOrEmpty(community))
            sb.AppendFormat(" AND (community LIKE '%{0}%')", community);
        if (!string.IsNullOrEmpty(region))
            sb.AppendFormat(" AND (region LIKE '%{0}%')", region);
        if (!string.IsNullOrEmpty(block))
            sb.AppendFormat(" AND (block LIKE '%{0}%')", block);
        if (!string.IsNullOrEmpty(houseuse))
            sb.AppendFormat(" AND (houseuse LIKE '%{0}%')", houseuse);
        if (!string.IsNullOrEmpty(buildingtype))
            sb.AppendFormat(" AND (buildingtype LIKE '%{0}%')", buildingtype);
       */
        
       // if (!string.IsNullOrEmpty(community))
       //     sb.AppendFormat(" AND (community LIKE '%{0}%')", community);
        
        StringBuilder sbOrder = new StringBuilder();

        //string col = type;
        if (string.IsNullOrEmpty(type))
        {
            type ="Community";
        }
       
        sbOrder.AppendFormat(" {0} DESC ", type);
        System.Data.DataSet dslist = null;
        string fieldlist = "Name,Num,HouseAddress,Region,trans";
        string tablefieldlist = string.Format("{0} as Name,count(*) as Num,HouseAddress,Region,sum(case when IsTransformed=1 then 1 else 0 end) as trans", type);
        sb.AppendFormat(" group by {0},HouseAddress,Region ", type);
        dslist = Chenxin.Lib.House.DAL.House_PublicDatum.GetList(
                iPageIndex, iPageSize, sbOrder.ToString(), "House_PublicDatum", sb.ToString(), fieldlist, tablefieldlist, true);

        DataSet ds = Chenxin.Lib.House.DAL.House_PublicDatum.GetList("", type);

        int count = ds.Tables[0].Rows.Count;
        
        string strHtml = "";
        if (!GetListHtml(dslist, out strHtml))
        {
            return string.Format("{{success:'0',msg:'{0}',data:''}}", "生成HTML错误");
        }

        dslist.Dispose();
        dslist = null;
        return string.Format("{{success:'1',msg:'{0}',data:{{count:'{1}',data:'{2}'}}}}", "成功", count, strHtml);
    }

    private bool GetListHtml(System.Data.DataSet dslist, out string strHtml)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<table width=\"100%\" height=\"100%\" cellspacing=\"1\" class=\"tabsorternew\" id=\"report\">");
        sb.Append("<thead>");
        sb.Append("<tr>");
        sb.Append("<th width=\"100\">分组类别</th>");
        sb.Append("<th width=\"80\" >总数量</th>");
        sb.Append("<th width=\"80\" >区域</th>");
        sb.Append("<th width=\"80\" >已转换数量</th>");
        sb.Append("<th width=\"100\">地址</th>");
        sb.Append("</tr></thead>");
        sb.Append("<tbody>");
        int i = 0;
        foreach (System.Data.DataRow row in dslist.Tables[0].Rows)
        {
            if (i % 2 == 0)
                sb.AppendFormat("<tr id=\"{0}\" class=\"tr_favorlist\">", row["Name"].ToString());
            else
                sb.AppendFormat("<tr id=\"{0}\" class=\"tr_houselist bluebgcolor\">", row["Name"].ToString());
            sb.AppendFormat("<td>{0}</td>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "Name", ""));
            sb.AppendFormat("<td>{0}</td>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "Num", ""));
            sb.AppendFormat("<td>{0}</td>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "Region", ""));
            sb.AppendFormat("<td>{0}</td>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "trans", ""));
            sb.AppendFormat("<td>{0}</td>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "HouseAddress", ""));

            sb.Append("</tr>");
            i++;
        }

        sb.Append("</tbody></table>");

        strHtml = sb.ToString();
        return true;
    }
}